/*
 * DrawerDialog.java
 *
 * Created on Oct 7, 2011, 9:52:30 PM
 */
package com.photoexpert;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JColorChooser;
import javax.swing.JPanel;

/**
 *
 * @author PANYIXIN
 */
class DrawerDialog extends javax.swing.JDialog {

    /** Creates new form DrawerDialog */
    DrawerDialog() {
        super(App.Instance(), false);
        initComponents();
        setLocation(200, 200);
        Presentor.Instance().setMouseMode(Presentor.MOUSE_MODES.DRAWING);
        _drawer.setDialog(this);
        _drawer.setMethod(MDrawer.METHODS.LINE);
        setVisible(true);
        _preview.repaint();
    }

    void colorChanged() {
        Color c = MDrawer.Instance().getColor();
        int alpha = c.getAlpha();
        _alpha.setText(new Integer(alpha).toString());
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        _colorLabel.setText("(" + new Integer(r).toString() + ","
            + new Integer(g).toString() + "," + new Integer(b).toString() + ")");
        setVisible(true);
        _preview.repaint();
    }

    class DrawerPreviewPane extends JPanel {
        @Override
        public void paint(Graphics g) {
            Dimension d = _preview.getSize();
            int width = d.width;
            int height = d.height;
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, width, height);
            g.setColor(_drawer.getColor());
            switch(_drawer.getMethod()) {
                case LINE :
                    g.drawLine(0, 0, width - 1, height - 1);
                    break;
                case RECT :
                    g.fillRect(width / 4, height / 4, width / 2, height / 2);
                    break;
                case OVAL :
                    g.fillOval(width / 4, height / 4, width  / 2, height / 2);
                    break;
                case FILL :
                    g.fillRect(0, 0, width, height);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _objectLabel = new javax.swing.JLabel();
        _object = new javax.swing.JComboBox();
        _preview = new DrawerPreviewPane();
        _colorChooser = new javax.swing.JButton();
        _colorPicker = new javax.swing.JButton();
        _close = new javax.swing.JButton();
        _alphaLabel = new javax.swing.JLabel();
        _alpha = new javax.swing.JTextField();
        _hint = new javax.swing.JLabel();
        _colorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Draw tools");
        setResizable(false);

        _objectLabel.setText("Draw object:");

        _object.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Line", "Rectangle", "Oval", "Fill" }));
        _object.setMaximumSize(new java.awt.Dimension(75, 19));
        _object.setMinimumSize(new java.awt.Dimension(75, 19));
        _object.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                _objectItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout _previewLayout = new javax.swing.GroupLayout(_preview);
        _preview.setLayout(_previewLayout);
        _previewLayout.setHorizontalGroup(
            _previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 249, Short.MAX_VALUE)
        );
        _previewLayout.setVerticalGroup(
            _previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 181, Short.MAX_VALUE)
        );

        _colorChooser.setText("Choose color");
        _colorChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _colorChooserActionPerformed(evt);
            }
        });

        _colorPicker.setText("Pick color");
        _colorPicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _colorPickerActionPerformed(evt);
            }
        });

        _close.setText("Close");
        _close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _closeActionPerformed(evt);
            }
        });

        _alphaLabel.setText("Alpha:");

        _alpha.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        _alpha.setText("255");
        _alpha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _alphaFocusLost(evt);
            }
        });

        _hint.setText("Draw line with mouse in the main screen          ");

        _colorLabel.setText("(0, 0, 0)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_hint, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(_objectLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_object, 0, 181, Short.MAX_VALUE))
                            .addComponent(_preview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_close, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                            .addComponent(_colorChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(_colorPicker, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                            .addComponent(_colorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(_alphaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(_alpha, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_objectLabel)
                    .addComponent(_object, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_colorChooser)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_colorPicker)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_colorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(_alphaLabel)
                            .addComponent(_alpha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(66, 66, 66)
                        .addComponent(_close, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(_preview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_hint, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__closeActionPerformed
        Presentor.Instance().setMouseMode(Presentor.MOUSE_MODES.DRAGGING);
        _drawer.setDialog(null);
        setVisible(false);
        dispose();
    }//GEN-LAST:event__closeActionPerformed

    private void _objectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event__objectItemStateChanged
        String drawObject = (String)_object.getSelectedItem();
        if(drawObject.equals("Line")) {
            _drawer.setMethod(MDrawer.METHODS.LINE);
            _hint.setText("Draw line with mouse in the main screen          ");
        }
        else if(drawObject.equals("Rectangle")) {
            _drawer.setMethod(MDrawer.METHODS.RECT);
            _hint.setText("Draw Rectangle with mouse in the main screen     ");
        }
        else if(drawObject.equals("Oval")) {
            _drawer.setMethod(MDrawer.METHODS.OVAL);
            _hint.setText("Draw oval with mouse in the main screen          ");
        }
        else if(drawObject.equals("Fill")) {
            _drawer.setMethod(MDrawer.METHODS.FILL);
            _hint.setText("Fill range of color with mouse in the main screen");
        }
        _preview.repaint();
    }//GEN-LAST:event__objectItemStateChanged

    private void _colorChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__colorChooserActionPerformed
        Color c = JColorChooser.showDialog(null, "Photo Expert", _drawer.getColor());
        if(c != null) {
            _drawer.setColor(c);
            int alpha = c.getAlpha();
            _alpha.setText(new Integer(alpha).toString());
            int r = c.getRed();
            int g = c.getGreen();
            int b = c.getBlue();
            _colorLabel.setText("(" + new Integer(r).toString() + ","
                + new Integer(g).toString() + "," + new Integer(b).toString() + ")");
            _preview.repaint();
        }
    }//GEN-LAST:event__colorChooserActionPerformed

    private void _colorPickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__colorPickerActionPerformed
        _drawer.setMethod(MDrawer.METHODS.COLOR_PICKER);
        //setVisible(false);
    }//GEN-LAST:event__colorPickerActionPerformed

    private void _alphaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__alphaFocusLost
        int a = Integer.parseInt(_alpha.getText());
        int rgb = _drawer.getColor().getRGB();
        int r = (rgb & 0x00ff0000) >> 16;
        int g = (rgb & 0x0000ff00) >> 8;
        int b = (rgb & 0x000000ff);
        Color c = new Color(r, g, b, a);
        _drawer.setColor(c);
        _preview.repaint();
    }//GEN-LAST:event__alphaFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField _alpha;
    private javax.swing.JLabel _alphaLabel;
    private javax.swing.JButton _close;
    private javax.swing.JButton _colorChooser;
    private javax.swing.JLabel _colorLabel;
    private javax.swing.JButton _colorPicker;
    private javax.swing.JLabel _hint;
    private javax.swing.JComboBox _object;
    private javax.swing.JLabel _objectLabel;
    private javax.swing.JPanel _preview;
    // End of variables declaration//GEN-END:variables
    private MDrawer _drawer = MDrawer.Instance();
}
